/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 播放列表页面视图测试
 */

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:music_example/viewmodels/music_player_view_model.dart';
import 'package:music_example/models/music_item_model.dart';
import 'package:music_example/pages/playlist_page_view.dart';

void main() {
  group('PlaylistPageView 测试', () {
    late MusicPlayerViewModel mockViewModel;

    setUp(() {
      mockViewModel = MusicPlayerViewModel();
    });

    tearDown(() {
      mockViewModel.dispose();
    });

    Widget createTestWidget({List<MusicItemModel>? playlist}) {
      if (playlist != null) {
        mockViewModel.setPlaylist(playlist);
      }

      return MaterialApp(
        home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
          value: mockViewModel,
          child: const PlaylistPageView(),
        ),
      );
    }

    testWidgets('空播放列表时显示空状态', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证空状态显示
      expect(find.text('播放列表为空'), findsOneWidget);
      expect(find.text('请先添加音乐到播放列表'), findsOneWidget);
      expect(find.byIcon(Icons.queue_music_rounded), findsOneWidget);
    });

    testWidgets('有播放列表时显示 PageView', (WidgetTester tester) async {
      final playlist = [
        const MusicItemModel(
          id: 'test_1',
          title: '测试歌曲 1',
          artist: '测试艺术家 1',
          album: '测试专辑 1',
          audioUrl: 'https://example.com/test1.mp3',
          sourceType: 'test',
        ),
        const MusicItemModel(
          id: 'test_2',
          title: '测试歌曲 2',
          artist: '测试艺术家 2',
          album: '测试专辑 2',
          audioUrl: 'https://example.com/test2.mp3',
          sourceType: 'test',
        ),
      ];

      await tester.pumpWidget(createTestWidget(playlist: playlist));

      // 验证 PageView 存在
      expect(find.byType(PageView), findsOneWidget);

      // 验证播放列表管理按钮存在
      expect(find.byType(FloatingActionButton), findsOneWidget);
      expect(find.byIcon(Icons.playlist_play), findsOneWidget);
    });

    testWidgets('播放列表管理按钮功能', (WidgetTester tester) async {
      final playlist = [
        const MusicItemModel(
          id: 'test_1',
          title: '测试歌曲 1',
          artist: '测试艺术家 1',
          album: '测试专辑 1',
          audioUrl: 'https://example.com/test1.mp3',
          sourceType: 'test',
        ),
      ];

      await tester.pumpWidget(createTestWidget(playlist: playlist));

      // 点击播放列表管理按钮
      await tester.tap(find.byType(FloatingActionButton));
      await tester.pumpAndSettle();

      // 验证底部面板显示
      expect(find.text('播放列表'), findsOneWidget);
      expect(find.text('共 1 首'), findsOneWidget);
      expect(find.text('清空'), findsOneWidget);
    });

    testWidgets('播放列表管理面板显示歌曲列表', (WidgetTester tester) async {
      final playlist = [
        const MusicItemModel(
          id: 'test_1',
          title: '测试歌曲 1',
          artist: '测试艺术家 1',
          album: '测试专辑 1',
          audioUrl: 'https://example.com/test1.mp3',
          sourceType: 'test',
        ),
        const MusicItemModel(
          id: 'test_2',
          title: '测试歌曲 2',
          artist: '测试艺术家 2',
          album: '测试专辑 2',
          audioUrl: 'https://example.com/test2.mp3',
          sourceType: 'test',
        ),
      ];

      await tester.pumpWidget(createTestWidget(playlist: playlist));

      // 打开播放列表管理面板
      await tester.tap(find.byType(FloatingActionButton));
      await tester.pumpAndSettle();

      // 验证歌曲列表显示
      expect(find.text('测试歌曲 1'), findsOneWidget);
      expect(find.text('测试艺术家 1'), findsOneWidget);
      expect(find.text('测试歌曲 2'), findsOneWidget);
      expect(find.text('测试艺术家 2'), findsOneWidget);
    });

    testWidgets('清空播放列表功能', (WidgetTester tester) async {
      final playlist = [
        const MusicItemModel(
          id: 'test_1',
          title: '测试歌曲 1',
          artist: '测试艺术家 1',
          album: '测试专辑 1',
          audioUrl: 'https://example.com/test1.mp3',
          sourceType: 'test',
        ),
      ];

      await tester.pumpWidget(createTestWidget(playlist: playlist));

      // 打开播放列表管理面板
      await tester.tap(find.byType(FloatingActionButton));
      await tester.pumpAndSettle();

      // 点击清空按钮
      await tester.tap(find.text('清空'));
      await tester.pumpAndSettle();

      // 确认清空对话框
      expect(find.text('清空播放列表'), findsOneWidget);
      expect(find.text('确定要清空所有播放列表吗？此操作不可撤销。'), findsOneWidget);

      // 点击确认清空
      await tester.tap(find.text('清空').last);
      await tester.pumpAndSettle();

      // 验证播放列表已清空
      expect(mockViewModel.playlist.isEmpty, true);
    });

    testWidgets('从播放列表移除单首歌曲', (WidgetTester tester) async {
      final playlist = [
        const MusicItemModel(
          id: 'test_1',
          title: '测试歌曲 1',
          artist: '测试艺术家 1',
          album: '测试专辑 1',
          audioUrl: 'https://example.com/test1.mp3',
          sourceType: 'test',
        ),
        const MusicItemModel(
          id: 'test_2',
          title: '测试歌曲 2',
          artist: '测试艺术家 2',
          album: '测试专辑 2',
          audioUrl: 'https://example.com/test2.mp3',
          sourceType: 'test',
        ),
      ];

      await tester.pumpWidget(createTestWidget(playlist: playlist));

      // 打开播放列表管理面板
      await tester.tap(find.byType(FloatingActionButton));
      await tester.pumpAndSettle();

      // 点击第一首歌的删除按钮
      await tester.tap(find.byIcon(Icons.close).first);
      await tester.pumpAndSettle();

      // 验证歌曲已从播放列表移除
      expect(mockViewModel.playlist.length, 1);
      expect(mockViewModel.playlist.first.title, '测试歌曲 2');
    });
  });

  group('PlaylistManagementSheet 测试', () {
    late MusicPlayerViewModel mockViewModel;

    setUp(() {
      mockViewModel = MusicPlayerViewModel();
    });

    tearDown(() {
      mockViewModel.dispose();
    });

    Widget createTestWidget({List<MusicItemModel>? playlist}) {
      if (playlist != null) {
        mockViewModel.setPlaylist(playlist);
      }

      return MaterialApp(
        home: ChangeNotifierProvider<MusicPlayerViewModel>.value(
          value: mockViewModel,
          child: Scaffold(
            body: Builder(
              builder: (context) => ElevatedButton(
                onPressed: () {
                  showModalBottomSheet(
                    context: context,
                    backgroundColor: Colors.transparent,
                    builder: (context) => PlaylistManagementSheet(
                      viewModel: mockViewModel,
                    ),
                  );
                },
                child: const Text('打开面板'),
              ),
            ),
          ),
        ),
      );
    }

    testWidgets('播放列表管理面板基本显示', (WidgetTester tester) async {
      final playlist = [
        const MusicItemModel(
          id: 'test_1',
          title: '测试歌曲 1',
          artist: '测试艺术家 1',
          album: '测试专辑 1',
          audioUrl: 'https://example.com/test1.mp3',
          sourceType: 'test',
        ),
      ];

      await tester.pumpWidget(createTestWidget(playlist: playlist));

      // 打开面板
      await tester.tap(find.text('打开面板'));
      await tester.pumpAndSettle();

      // 验证面板内容
      expect(find.text('播放列表'), findsOneWidget);
      expect(find.text('共 1 首'), findsOneWidget);
      expect(find.text('清空'), findsOneWidget);
      expect(find.text('测试歌曲 1'), findsOneWidget);
      expect(find.text('测试艺术家 1'), findsOneWidget);
    });
  });
}
